OUTPUT=labdoo-mini.iso

CD_DIR=cd
INITRD_ADD_DIR=add
ORIGINAL=orig/mini.iso

ISOLINUX_BINARIES=$(CD_DIR)/boot.cat $(CD_DIR)/isolinux.bin \
		$(CD_DIR)/linux $(CD_DIR)/vesamenu.c32

INCLUDE=$(CD_DIR)/.disk

DEPS=$(ISOLINUX_BINARIES) $(INCLUDE) $(CD_DIR)/initrd.gz $(CD_DIR)/labdoo.png $(CD_DIR)/f1.txt

MINI_ISOS="https://help.ubuntu.com/community/Installation/MinimalCD"
ISO_URL="http://archive.ubuntu.com/ubuntu/dists/precise/main/installer-i386/current/images/netboot/mini.iso"

SPLASH_URL="http://labdoo-iso.googlecode.com/svn/trunk/doc/isolinux/img/splab1.png"
TXT_URL="https://labdoo-iso.googlecode.com/svn/trunk/doc/isolinux/txt"

define inflate
	@echo Inflating $(1) from $(ORIGINAL) into $(2)
	7z x $(ORIGINAL) -o$(2) $(1) | grep -oP '(?<=^Extracting).*$$' | sed 's/^ */$(2)\//g' | xargs touch     
endef


.DEFAULT_TARGET: all
.PHONY: all clean-all clean

all: $(OUTPUT)


$(OUTPUT): $(shell find $(CD_DIR)) $(DEPS)
	cd $(CD_DIR) ;\
	mkisofs -o ../$@ -r -J -no-emul-boot -boot-load-size 4 \
	-boot-info-table -b isolinux.bin -c boot.cat .


$(CD_DIR)/initrd.gz: $(shell find $(INITRD_ADD_DIR)) $(ORIGINAL)
	rm -f $@ $(CD_DIR)/initrd
	$(call inflate,initrd.gz,$(CD_DIR))
	gzip -d $@

	cd $(INITRD_ADD_DIR) ;\
	find . | fakeroot cpio --quiet -F ../$(CD_DIR)/initrd --append -o -H newc

	gzip $(CD_DIR)/initrd


$(ISOLINUX_BINARIES): $(ORIGINAL)
	$(call inflate,$(notdir $@),$(CD_DIR))


$(INCLUDE): $(ORIGINAL)
	$(call inflate,$(shell echo $@ | sed "s/$(CD_DIR)\///"),$(CD_DIR))


$(ORIGINAL):
	wget -O $@ $(ISO_URL)


$(CD_DIR)/labdoo.png:
	wget -O $@ $(SPLASH_URL)


$(CD_DIR)/f1.txt:
	svn export --force $(TXT_URL) $(CD_DIR)


clean-all: clean
	rm -rf $(DEPS) $(CD_DIR)/f*.txt $(ORIGINAL)


clean:
	rm -f $(CD_DIR)/initrd.gz $(CD_DIR)/initrd $(OUTPUT)
